﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using Sinacor.OMM.Receptor.CommunicationChannel;
using Sinacor.OMM.Logging;

namespace Sinacor.OMM.Receptor
{
    partial class ReceptorService : ServiceBase
    {

        STMChannel _channel;
        Sinacor.OMM.HealthMonitor.Collector.HealthMonitorCollector _healthMonitor;
        public ReceptorService()
        {
            InitializeComponent();
            this.ServiceName = "BM&FBOVESPA Receptor Service";
        }


        /// <summary>
        /// On Start
        /// </summary>
        /// <param name="args"></param>
        protected override void OnStart(string[] args)
        {
            _healthMonitor = Sinacor.OMM.HealthMonitor.Collector.HealthMonitorCollector.GetInstance();

            if (ReceptorConfigurationManager.ReceptorConfiguration.CommunicationChannels.Count > 0)
            {
                _channel = new STMChannel(ReceptorConfigurationManager.ReceptorConfiguration.CommunicationChannels[0], ReceptorConfigurationManager.OrderBovespaSystemInformation);
                _channel.Start();
            }
            else
            {                
                EventLog.WriteEntry(LogSources.ReceptorService, "Receptor Configuration not found.", EventLogEntryType.Error);
                this.Stop();
            }
        }

        /// <summary>
        /// On Stop
        /// </summary>
        protected override void OnStop()
        {
            _channel.Dispose();
            _healthMonitor.Dispose();
        }
    }
}
